from flask import Blueprint, request, render_template, make_response, redirect, session, flash
# from werkzeug.security import generate_password_hash, check_password_hash
from model import *
from utils.captcha.captcha import captcha
from news.checklogin import checklogin

web = Blueprint ("web", __name__)

"""===================================================用户注册========================================================"""

@web.route ("/reg", methods = ["GET", "POST"])
def reg ():
    if request.method == "GET":
        return render_template ("web/register.html")
    else:
        name = request.form.get ("name")
        password = request.form.get ("password")
        phone = request.form.get ("phone")
        photo = request.files.get ("photo")
        code = request.form.get ("code")
        if User.query.filter (User.username == name).all ():
            flash ("该账户已存在，请重现注册。。。。。。")
            return redirect ("/reg")
        index = photo.filename.rfind (".")
        type1 = photo.filename [index + 1:]
        if type1 not in ["jpg", "fig", "jpeg", "png"]:
            return redirect ("reg")
        photo.save ("static/upload/" + photo.filename)
        user = User (username = name, password = "password",
                     phone = phone, photo = "/static/upload/" + photo.filename)
        db.session.add (user)
        db.session.commit ()
        return redirect ("/login")

"""===================================================用户登录========================================================"""

@web.route ("/login", methods = ["GET", "POST"])
def login ():
    if request.method == "GET":
        return render_template ("web/login.html")
    else:
        username = request.form.get ("username")
        password = request.form.get ("password")
        user_name = User.query.filter (User.username == username,User.password == password).first ()
        users = User.query.first ()
        if user_name:
            session["uid"] = user_name.id
            session ["username"] = user_name.username
            session ["photo"] = user_name.photo
            return render_template ("web/index.html", users = users)
        else:
            flash ("账号或密码错误，请重新输入。。。。。。")
            return render_template ("web/login.html")

"""===================================================首页==========================================================="""

@web.route ("/index")
@checklogin
def index ():
    user = User.query.all ()
    return render_template ("web/index.html", user = user)

"""===================================================动态验证码======================================================"""

# 产生图片验证码
@web.route ("/code")
def makecode ():
    name, text, img = captcha.generate_captcha ()
    response = make_response (img)
    response.headers ["Content-Type"] = "image/jpg"
    return response

"""===================================================推出==========================================================="""

@web.route ("/outlogin")
def outlogin ():
    session.clear ()
    return redirect ("/login")
